package com.example.fw;

import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Reporter;

public class GroupHelper extends HelperWithWebDriverBase {
	
	//private Groups groupCache;
	private	Groups groupModel;	// lesson 7 - 57:00, model instead of cache
	private	int nextGroupId;
	
	public GroupHelper(ApplicationManager manager) {		// constructor GroupHelper; Assign parameter to new field for "manager"
		super(manager);										// init super class HelperWithWebDriverBase
		groupModel = getGroups();
		nextGroupId = Integer.parseInt(manager.getHibernateHelper().getNextGroupId());
	}
	
	public Groups getGroups() {		// groupCache was at lesson 4 ~ 53:00
		if (groupModel == null)	{
			groupModel = manager.getHibernateHelper().getGroups();
		}
		return groupModel;
	}
		
	public Groups getUiGroups() {		// lesson 7 ~ 72:00
		HashSet<GroupObject> set = new HashSet<GroupObject>();
		manager.getNavigationHelper().goToGroupListPage();
		WebElement form = findElements(By.tagName("form")).get(1);
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			// lesson 4 regular 40:00
			Pattern p = Pattern.compile(".*\\((.*)\\)");	// lesson 4 47:00 - find all elements (symbols) inside ( )
			Matcher m = p.matcher(title);
			if (m.matches())	{
				title = m.group(1);
			}
			GroupObject group = new GroupObject()
								.setName(title)
								.setId(checkbox.getAttribute("value"));
			set.add(group);
		}
		return new Groups(set);
	}

//	private void buildGroupCache()	{ 
//		HashSet<GroupObject> set = new HashSet<GroupObject>();
//		manager.getNavigationHelper().goToGroupListPage();
//		WebElement form = findElements(By.tagName("form")).get(1);
//		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
//		for (WebElement checkbox : checkboxes) {
//			String title = checkbox.getAttribute("title");
//			// lesson 4 regular 40:00
//			Pattern p = Pattern.compile(".*\\((.*)\\)");	// lesson 4 47:00 - find all elements (symbols) inside ( )
//			Matcher m = p.matcher(title);
//			if (m.matches())	{
//				title = m.group(1);
//				//System.out.println(m.group(1));		// sysout			
//			}
//			
//			// lesson 4 31:00
//			//int startIndex = title.indexOf('(');
//			//int endIndex = title.lastIndexOf(')');
//			//title = title.substring(startIndex, endIndex);
//			//title = title.substring("Select (".length(), title.length()-")".length());				//	lesson 3 - 35:00
//			
//			GroupObject group = new GroupObject()
//								.setName(title)
//								.setId(checkbox.getAttribute("value"));
//			set.add(group);
//			//groupCache.add(group);
//		}
//		groupCache = new Groups(set);
//		//return groups;
//	}
	
	public void createGroup(GroupObject groupObject) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupCreation();
		fillGroupForm(groupObject);
		submitGroupCreation();
		returnToGroupListPage();	// here
		//buildGroupCache();
		groupObject.setId("" + nextGroupId);
		nextGroupId = nextGroupId + 1;
		groupModel = groupModel.withAdded(groupObject);
	}

	public void modifyGroup(GroupObject groupBeforeModification, GroupObject groupAfterModification) {
		manager.getNavigationHelper().goToGroupListPage();
		initGroupModification(groupBeforeModification);
		fillGroupForm(groupAfterModification);
		submitUpdate();
		returnToGroupListPage();
		//buildGroupCache();
		groupModel = groupModel.withOut(groupBeforeModification).withAdded(groupAfterModification);
	}
	
	public void deleteGroup(GroupObject someGroup) {
		manager.getNavigationHelper().goToGroupListPage();
		selectGroup(someGroup);
		submitDelete();
		returnToGroupListPage();
		//buildGroupCache();
		groupModel = groupModel.withOut(someGroup);
	}

	private void initGroupModification(GroupObject someGroup) {
		selectGroup(someGroup);
		submitEdit();
	}

	private void returnToGroupListPage() {
		click(By.linkText("group page"));
	}

	private void fillGroupForm(GroupObject groupObject) {
		Reporter.log("filling group form");
		type(By.name("group_name"), groupObject.name);
		type(By.name("group_header"), groupObject.header);
		type(By.name("group_footer"), groupObject.footer);
	}
	
	private void initGroupCreation() {
		click(By.name("new"));
	}

	private void submitDelete() {
		findElement(By.name("delete")).click();
	}

	private void submitEdit() {
		findElement(By.name("edit")).click();
	}

	private void submitUpdate() {
		findElement(By.name("update")).click();
	}

	private void selectGroup(GroupObject someGroup) {		//alternate at lesson 7, 61:30
		
		WebElement form = findElements(By.tagName("form")).get(1);
		List<WebElement> checkboxes = form.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
		
			String title = checkbox.getAttribute("title");
			title = title.substring("Select (".length(), title.length()-")".length());
			
			if (title.equals(someGroup.name)) {
				checkbox.click();
				return;
			}
		}
	}
	
	void submitGroupCreation() {
		driver.findElement(By.name("submit")).click();
	}
}